package org.jivesoftware.openfire.findpwd;

import java.io.File;

import org.jivesoftware.openfire.XMPPServer;
import org.jivesoftware.openfire.container.Plugin;
import org.jivesoftware.openfire.container.PluginManager;
import org.jivesoftware.openfire.interceptor.InterceptorManager;
import org.jivesoftware.openfire.interceptor.PacketInterceptor;
import org.jivesoftware.openfire.interceptor.PacketRejectedException;

import org.jivesoftware.openfire.session.Session;
import org.jivesoftware.openfire.user.UserManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xmpp.packet.JID;
import org.xmpp.packet.Message;
import org.xmpp.packet.Packet;

public class FindPwdPlugin implements PacketInterceptor, Plugin{
	private static final Logger log = LoggerFactory.getLogger(FindPwdPlugin.class);
	 private static PluginManager pluginManager;
  private static  DbFindPwdManager findPwdManager;
  private InterceptorManager interceptorManager;
  private static final String emailReceiver = "email";
  public FindPwdPlugin(){
      interceptorManager = InterceptorManager.getInstance();
      findPwdManager = DbFindPwdManager.getInstance();
  }
  @Override
  public void initializePlugin(PluginManager manager, File pluginDirectory) {
  	// TODO Auto-generated method stub
  	 interceptorManager.addInterceptor(this);
       pluginManager = manager;
       log.debug("FindPwdPlugin  init success");
  }
@Override
public void destroyPlugin() {
	// TODO Auto-generated method stub
	interceptorManager.removeInterceptor(this);
    log.debug("FindPwdPlugin destroy success");
}
@Override
public void interceptPacket(Packet packet, Session session, boolean incoming,
		boolean processed) throws PacketRejectedException {
	// TODO Auto-generated method stub
	  JID recipient = packet.getTo();
      if (recipient != null) {
          String username = recipient.getNode();
          if (username == null || !UserManager.getInstance().isRegisteredUser(recipient)) {
              return;
          } else if (!XMPPServer.getInstance().getServerInfo().getXMPPDomain().equals(recipient.getDomain())) {
              return;
          } else if ("".equals(recipient.getResource())) {
          }
      }
      this.doAction(packet, incoming, processed, session);
}
	private void doAction(Packet packet, boolean incoming, boolean processed,
		Session session) {
	// TODO Auto-generated method stub
		Packet copyPacket = packet.createCopy();
		  if (packet instanceof Message) {		
	           Message message = (Message) copyPacket;
	           if(message.getType()==Message.Type.chat){//single
	          	 log.debug("debug","single chat"+message.toXML());
	           }
	         if (processed || !incoming) {
	             return;
	         }
	         if (incoming) {       
	             JID recipient = message.getTo();
	             String receiver = recipient.getNode();
	             if(receiver.equals(emailReceiver)){           
	            	try {
	            		FindPwdBean findPwdBean = new FindPwdBean();
	            		 findPwdBean = findPwdManager.queryFindPwd(getEmail(packet, incoming, session));
	    
						EmailUtils.sendEmail(findPwdBean);
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
	             } 
	  
	         }
		  }
	}
	private String getEmail(Packet packet, boolean incoming, Session session) {
		// TODO Auto-generated method stub
		 Message message = (Message) packet;
		   String body = message.getBody();
		   log.info("body:"+body);
		return body;
	}
}
